package cn.yj.core.business.service;

import cn.yj.cgs.dba.mapper.ArticleMapper;
import cn.yj.cgs.dba.mapper.PackagesMapper;
import cn.yj.cgs.dba.mapper.ProductMapper;
import cn.yj.cgs.dba.mapper.TaskMapper;
import cn.yj.common.NameValue;
import cn.yj.commons.utils.DateTimeUtils;
import cn.yj.core.business.service.i.IStatistical;
import cn.yj.syslog.service.IPageViewService;
import com.google.common.collect.Maps;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author 永健
 * @since 2021-06-16 09:52
 */
@Service
public class StatisticalService implements IStatistical {


    @Resource
    private IPageViewService sysLogService;

    @Resource
    private CustomerService customerService;

    @Resource
    private TaskMapper taskMapper;

    @Resource
    private PackagesMapper packagesMapper;

    @Resource
    private ArticleMapper articleMapper;

    @Resource
    private ProductMapper productMapper;

    /**
     * 活动量
     */
    @Override
    public Map<String, Object> activity() {

        Date toDay = new Date();

        // 日活跃用户 今天
        long dayCount = sysLogService.countByDate(toDay);
        long allCount = sysLogService.allCount();

        // 周活量
        Date weekDay = DateTimeUtils.addDays(new Date(), -7);
        long weekCount = sysLogService.countByDateRange(weekDay, toDay);

        // 月活动量
        Date monthFirstDay = DateTimeUtils.getMonthFirstDay();
        Date monthLastDay = DateTimeUtils.getMonthLastDay();
        long monthCount = sysLogService.countByDateRange(monthFirstDay, monthLastDay);

        HashMap<String, Object> map = Maps.newHashMap();
        map.put("dayCount", dayCount);
        map.put("weekCount", weekCount);
        map.put("monthCount", monthCount);
        map.put("allCount", allCount);

        int first=Integer.parseInt(String .format("%td",monthFirstDay));
        int last=Integer.parseInt(String .format("%td",new Date()));
        List<String> dayList = new ArrayList<>();
        List<Long> dayVisitList = new ArrayList<>();
        // 1号
        dayVisitList.add(sysLogService.countByDate(monthFirstDay));

        for (int i = first; i <= last; i++) {
            dayList.add(i+"号");
            if (i>first&&i<last){
                dayVisitList.add(sysLogService.countByDate(DateTimeUtils.addDays(monthFirstDay,i-1)));
            }
        }
        dayVisitList.add(dayCount);
        map.put("date",dayList);
        map.put("dateCount",dayVisitList);
        return map;
    }

    @Override
    public Map<String, Object> userCount() {
        long allUserCount = customerService.countUser();

        Date toDay = new Date();
        long toDayCount = customerService.countByDate(toDay);

        Date weekDay = DateTimeUtils.addDays(toDay, -7);
        long weekCount = customerService.countByDateRange(weekDay, toDay);

        Date monthFirstDay = DateTimeUtils.getMonthFirstDay();
        Date monthLastDay = DateTimeUtils.getMonthLastDay();
        long monthCount = customerService.countByDateRange(monthFirstDay, monthLastDay);

        HashMap<String, Object> map = Maps.newHashMap();
        map.put("allCount", allUserCount);
        map.put("dayCount", toDayCount);
        map.put("weekCount", weekCount);
        map.put("monthCount", monthCount);
        return map;
    }

    @Override
    public Map<String, Object> taskCount() {
        long allCount = taskMapper.countAll();

        Date toDay = new Date();
        long toDayCount = taskMapper.countByDate(toDay);

        Date weekDay = DateTimeUtils.addDays(toDay, -7);
        long weekCount = taskMapper.countByDateRange(weekDay, toDay);

        Date monthFirstDay = DateTimeUtils.getMonthFirstDay();
        Date monthLastDay = DateTimeUtils.getMonthLastDay();
        long monthCount = taskMapper.countByDateRange(monthFirstDay, monthLastDay);

        HashMap<String, Object> map = Maps.newHashMap();
        map.put("allCount", allCount);
        map.put("dayCount", toDayCount);
        map.put("weekCount", weekCount);
        map.put("monthCount", monthCount);
        return map;
    }

    @Override
    public Map<String, Object> articleCount() {
        long allCount = articleMapper.countAll();

        Date toDay = new Date();
        long toDayCount = articleMapper.countByDate(toDay);

        Date weekDay = DateTimeUtils.addDays(toDay, -7);
        long weekCount = articleMapper.countByDateRange(weekDay, toDay);

        Date monthFirstDay = DateTimeUtils.getMonthFirstDay();
        Date monthLastDay = DateTimeUtils.getMonthLastDay();
        long monthCount = articleMapper.countByDateRange(monthFirstDay, monthLastDay);

        HashMap<String, Object> map = Maps.newHashMap();
        map.put("allCount", allCount);
        map.put("dayCount", toDayCount);
        map.put("weekCount", weekCount);
        map.put("monthCount", monthCount);
        return map;
    }

    @Override
    public Map<String, Object> packagesCount() {
        Date toDay = new Date();

        long toDayCount = packagesMapper.countByDate(toDay);
        Date weekDay = DateTimeUtils.addDays(toDay, -7);
        long weekCount = packagesMapper.countByDateRange(weekDay, toDay);

        Date monthFirstDay = DateTimeUtils.getMonthFirstDay();
        Date monthLastDay = DateTimeUtils.getMonthLastDay();
        long monthCount = packagesMapper.countByDateRange(monthFirstDay, monthLastDay);

        HashMap<String, Object> map = Maps.newHashMap();
        map.put("allCount", packagesMapper.countAll());
        map.put("dayCount", toDayCount);
        map.put("weekCount", weekCount);
        map.put("monthCount", monthCount);
        return map;
    }

    @Override
    public Map<String, Object> productCount() {
        Date toDay = new Date();

        long toDayCount = productMapper.countByDate(toDay);
        Date weekDay = DateTimeUtils.addDays(toDay, -7);
        long weekCount = productMapper.countByDateRange(weekDay, toDay);

        Date monthFirstDay = DateTimeUtils.getMonthFirstDay();
        Date monthLastDay = DateTimeUtils.getMonthLastDay();
        long monthCount = productMapper.countByDateRange(monthFirstDay, monthLastDay);

        HashMap<String, Object> map = Maps.newHashMap();
        map.put("allCount", productMapper.countAll());
        map.put("dayCount", toDayCount);
        map.put("weekCount", weekCount);
        map.put("monthCount", monthCount);
        return map;
    }

    @Override
    public List<NameValue> pieUser() {
        return customerService.pieChart();
    }

    @Override
    public List<NameValue> pieTask() {
        return taskMapper.pieChart();
    }

    @Override
    public List<NameValue> pieArticle() {
        return articleMapper.pieChart();
    }

    @Override
    public List<NameValue> pieProduct() {
        return productMapper.pieChart();
    }

    @Override
    public List<NameValue> piePackages() {
        return packagesMapper.pieChart();
    }

}
